﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.Domain.Services
{
    public class CreateService
    {
        public ServiceResult CreateMaintainer(string name, string email, string mobile,string startTime,string endTime)
        {
            if (string.IsNullOrEmpty(name)|| string.IsNullOrEmpty(email))
            {
                return new ServiceResult(false, ErrorCode.COM_InvalidParameter, ErrorCode.String(ErrorCode.COM_InvalidParameter));
            }

            using (EtlDbContext dbContext = new EtlDbContext())
            {
                Entities.Maintainer entity = new Entities.Maintainer();
                entity.Id = Guid.NewGuid();
                entity.Name = name;
                entity.Email = email;
                entity.Mobile = mobile;
                entity.StartTime = startTime;
                entity.EndTime = endTime;
                dbContext.Set<Entities.Maintainer>().Add(entity);
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult(true, ErrorCode.COM_NoError, string.Empty);
                }
                catch (Exception ex)
                {
                    return new ServiceResult(false, ErrorCode.COM_DBError, ex.Message);
                }
            }
        }


        public ServiceResult CreateIntegrityCheckInstance(Guid taskId, Guid integrityCheckId, string parameterNames, string parameterValues)
        {
            IntegrityCheck.IIntegrityCheck check =  IntegrityCheck.ICFactory.Create(integrityCheckId);
            if(check == null)
            {
                return new ServiceResult(false, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
            }

            List<IntegrityCheck.Parameter> parameterDefs = check.EnumParameters();

            string[] parameterNameArr = parameterNames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] parameterValueArr = parameterValues.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if(parameterValueArr.Length != parameterValueArr.Length
                && parameterDefs.Count != parameterNameArr.Length)
            {
                return new ServiceResult(false, ErrorCode.COM_InvalidParameter, ErrorCode.String(ErrorCode.COM_InvalidParameter));
            }

            using(EtlDbContext dbContext = new EtlDbContext())
            {
                Entities.IntegrityCheckInstance instance = new Entities.IntegrityCheckInstance();
                instance.Id = Guid.NewGuid();
                instance.TaskId = taskId;
                instance.IntegrityCheckId = integrityCheckId;
                instance.CreatedTime = DateTime.Now;

                dbContext.Set<Entities.IntegrityCheckInstance>().Add(instance);

                for (int index = 0; index < parameterNameArr.Length; index++)
                {
                    Entities.IntegrityCheckParameter parameter = new Entities.IntegrityCheckParameter();
                    parameter.Id = Guid.NewGuid();
                    parameter.InstanceId = instance.Id;
                    parameter.Name = parameterNameArr[index];
                    parameter.Value = parameterValueArr[index];
                    parameter.SortIndex = index;
                    dbContext.Set<Entities.IntegrityCheckParameter>().Add(parameter);
                }

                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch(Exception ex)
                {
                    return new ServiceResult(false, ErrorCode.COM_DBError, ex.Message);
                }
            }

        }

        public ServiceResult CreateTaskCategory(Guid? parentId, string code, string name)
        {
            using (EtlDbContext dbContext = new EtlDbContext())
            {
                if (parentId == null)
                {
                    Entities.ETaskCategory category = new Entities.ETaskCategory();
                    category.Id = Guid.NewGuid();
                    category.Code = code;
                    category.FullCode = code;
                    category.IsLeaf = true;
                    category.Level = 1;
                    category.Name = name;
                    dbContext.Set<Entities.ETaskCategory>().Add(category);
                }
                else
                {
                    Entities.ETaskCategory eParent = dbContext.Set<Entities.ETaskCategory>().SingleOrDefault(p => p.Id == parentId.Value);
                    if (eParent == null)
                    {
                        return new ServiceResult(false, ErrorCode.COM_OBJNotExist, ErrorCode.String(ErrorCode.COM_OBJNotExist));
                    }

                    eParent.IsLeaf = false;
                    dbContext.Entry<Entities.ETaskCategory>(eParent).State = System.Data.Entity.EntityState.Modified;

                    Entities.ETaskCategory eCategory = new Entities.ETaskCategory();
                    eCategory.Id = Guid.NewGuid();
                    eCategory.Code = code;
                    eCategory.Name = name;
                    eCategory.ParentId = eParent.Id;
                    eCategory.FullCode = string.Format("{0}{1}", eParent.FullCode, code);
                    eCategory.Level = eParent.Level + 1;
                    eCategory.IsLeaf = true;
                    dbContext.Set<Entities.ETaskCategory>().Add(eCategory);
                }
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    return new ServiceResult(false, ErrorCode.COM_DBError, ex.Message);
                }
            }
        }
    }
}
